package com.taiyuan.zfmvp.JUC.sync;

/**
 * @param
 * @Author: zhoufang@si-tech.com.cn
 * @Descriiption: 线程间通信  wait notify
 * @Date 2022/10/3
 * @return
 */

class share{

    private int number = 0;

    public synchronized  void incr() throws  Exception{
        while(number != 0){
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName()+"::"+number);
        this.notifyAll();

    }
    public synchronized  void decr() throws  Exception{
        while(number != 1){
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName()+"::"+number);
        this.notifyAll();
    }

}

public class ThreadDemo1 {

    public static void main(String[] args) {
        share s = new share();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 10 ; i++) {
                    try {
                        s.incr();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        },"a").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 10 ; i++) {
                    try {
                        s.decr();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        },"b").start();
    }

}
